﻿using QuanLySieuThi.Controllers;
using QuanLySieuThi.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace QuanLySieuThi.Views
{
    public partial class frmHangHoa : Form
    {
        public frmHangHoa()
        {
            InitializeComponent();

            dataGridView1.DataSource = QuanLyMatHang.UniqueObject.GetDataTable;
            Bindings();

        }

        private void frmHangHoa_Load(object sender, EventArgs e)
        {
            //dataGridView1.DataSource = null;

        }

        void Bindings()
        {

            //txtGiaBan.DataBindings.Clear();
            //txtGiaMua.DataBindings.Clear();
            //txtHSD.DataBindings.Clear();
            //txtLoaiMatHang.DataBindings.Clear();
            //txtMaMatHang.DataBindings.Clear();
            //txtNgaySX.DataBindings.Clear();
            //txtSoLuong.DataBindings.Clear();
            //txtTenMatHang.DataBindings.Clear();

            //txtGiaBan.DataBindings.Add("Text", dataGridView1.DataSource, "GiaBan");
            //txtGiaMua.DataBindings.Add("Text", dataGridView1.DataSource, "GiaMua");
            //txtHSD.DataBindings.Add("Value", dataGridView1.DataSource, "HanSuDung");
            //txtLoaiMatHang.DataBindings.Add("Text", dataGridView1.DataSource, "MaLoaiHang");
            //txtMaMatHang.DataBindings.Add("Text", dataGridView1.DataSource, "MaMatHang");
            //txtNgaySX.DataBindings.Add("Value", dataGridView1.DataSource, "NgaySanXuat");
            //txtSoLuong.DataBindings.Add("Text", dataGridView1.DataSource, "SoLuong");
            //txtTenMatHang.DataBindings.Add("Text", dataGridView1.DataSource, "TenMatHang");
            //txtSLBan.DataBindings.Add("Text", dataGridView1.DataSource, "SoLuongNhap");
            //txtSLMua.DataBindings.Add("Text", dataGridView1.DataSource, "SoLuongBan");
        }

        private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
        {

        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            txtGiaBan.Text = dataGridView1.Rows[e.RowIndex].Cells["GiaBan"].Value.ToString();
            txtGiaMua.Text = dataGridView1.Rows[e.RowIndex].Cells["GiaMua"].Value.ToString();
            if (dataGridView1.Rows[e.RowIndex].Cells["HanSuDung"].Value != null)
                txtHSD.Value = DateTime.Parse(dataGridView1.Rows[e.RowIndex].Cells["HanSuDung"].Value.ToString());

            txtLoaiMatHang.Text = dataGridView1.Rows[e.RowIndex].Cells["MaLoaiHang"].Value.ToString();
            txtMaMatHang.Text = dataGridView1.Rows[e.RowIndex].Cells["MaMatHang"].Value.ToString();

            if (dataGridView1.Rows[e.RowIndex].Cells["NgaySanXuat"].Value != null)
                txtNgaySX.Value = DateTime.Parse(dataGridView1.Rows[e.RowIndex].Cells["NgaySanXuat"].Value.ToString());

            txtSoLuong.Text = dataGridView1.Rows[e.RowIndex].Cells["SoLuong"].Value.ToString();
            txtTenMatHang.Text = dataGridView1.Rows[e.RowIndex].Cells["TenMatHang"].Value.ToString();
            txtSLBan.Text = dataGridView1.Rows[e.RowIndex].Cells["SoLuongBan"].Value.ToString();
            txtSLMua.Text = dataGridView1.Rows[e.RowIndex].Cells["SoLuongNhap"].Value.ToString();


        }

        private void clickCapNhat(object sender, EventArgs e)
        {
            try
            {
                MatHang h = GetMatHangFromView();
                QuanLyMatHang.UniqueObject.Update(h);
                //dataGridView1.Refresh();
                dataGridView1.DataSource = QuanLyMatHang.UniqueObject.GetDataTable;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void clickThemMoi(object sender, EventArgs e)
        {
            try
            {
                MatHang h = GetMatHangFromView();
                QuanLyMatHang.UniqueObject.Add(h);
                dataGridView1.DataSource = QuanLyMatHang.UniqueObject.GetDataTable;

               // dataGridView1.Refresh();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void clickXoa(object sender, EventArgs e)
        {
            var result = MessageBox.Show("Xóa mặt hàng "+txtTenMatHang.Text, "Chú ý",MessageBoxButtons.YesNo,MessageBoxIcon.Warning);
            if (result == System.Windows.Forms.DialogResult.Yes)
            {

                QuanLyMatHang.UniqueObject.Delete(txtMaMatHang.Text);
                //  dataGridView1.Refresh();
                dataGridView1.DataSource = QuanLyMatHang.UniqueObject.GetDataTable;
                //Bindings();
                dataGridView1.Refresh();
            } 

        }

        private MatHang GetMatHangFromView()
        {
            MatHang m = new MatHang();
            m.GiaBan = int.Parse(txtGiaBan.Text);
            m.GiaMua = int.Parse(txtGiaMua.Text);
            m.HanSuDung = txtHSD.Value;
            m.MaLoaiHang = txtLoaiMatHang.Text;
            m.MaMatHang = txtMaMatHang.Text;
            m.NgaySanXuat = txtNgaySX.Value;
            m.SoLuong = int.Parse(txtSoLuong.Text);
            m.TenMatHang = txtTenMatHang.Text;

            m.SoLuongBan = txtSoLuong.Text.Length > 0 ? int.Parse(txtSoLuong.Text) : 0;
            m.SoLuongNhap = txtSLMua.Text.Length > 0 ? int.Parse(txtSLMua.Text) : 0;
            return m;
        }

        private void txtTimKiem_TextChanged(object sender, EventArgs e)
        {
            dataGridView1.DataSource = QuanLyMatHang.UniqueObject.TimKiem(txtTimKiem.Text);
        }

        private void button4_Click(object sender, EventArgs e)
        {
            txtGiaBan.Text = "";
            txtGiaMua.Text = "";
            txtLoaiMatHang.Text = "";
            txtMaMatHang.Text = "";
            txtSoLuong.Text = "";
            txtTenMatHang.Text = "";
            txtSLBan.Text = "";
            txtSLMua.Text = "";
        }
    }
}
